import eventBus from "../../eventBus/eventBus";
export const hospitalInfos = {
  cacheData: null,
  cacheFormData: null,

  get properties() {
    return [
      "page",
      "limit",
      "hosName",
      "provinceName",
      "cityName",
      "type",
      "level",
    ];
  },

  isSameFormData(newData) {
    if (
      this.cacheFormData &&
      Object.keys(this.cacheFormData).length &&
      newData &&
      Object.keys(newData).length
    ) {
      for (const property of this.properties) {
        if (newData[property] !== this.cacheFormData[property]) {
          return false;
        }
      }
      return true;
    } else {
      return false;
    }
  },
  async getHospitalInfos(formData) {
    if (this.isSameFormData(formData)) {
      return this.cacheData;
    } else {
      try {
        const { data } = await eventBus.$axios.post(
          "proxy/getHospitalInfos",
          formData
        );
        this.cacheData = data;
        return data;
      } catch (error) {
        throw error;
      }
    }
  },
  async getHospitalNames(formData) {
    const hospitalList = await this.getHospitalInfos(formData);
    if (hospitalList?.length) {
      return hospitalList?.map((hospital) => hospital?.hosName);
    }
  },
};
